package com.example.chcrm.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.chcrm.entity.StatsDTO.ConsignorStatsDTO;
import com.example.chcrm.entity.StatsDTO.SupplierStatsDTO;
import com.example.chcrm.entity.StatsDTO.VehicleStatistics;
import com.example.chcrm.entity.common.ResponseMsg;
import com.example.chcrm.entity.common.ServletUtils;
import com.example.chcrm.entity.common.TArea;
import com.example.chcrm.entity.employee.EmployeeVo;
import com.example.chcrm.entity.vehicle.TransportChildCv;
import com.example.chcrm.entity.vehicle.TransportPlanCv;
import com.example.chcrm.entity.vehicle.VehicleDeployCarCv;
import com.example.chcrm.service.TransportChildCvService;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @Author 邹
 * @Date 2025/4/24 11:15
 * @Version 1.0
 */
@RestController
@RequestMapping("/transportChild")
@CrossOrigin // 允许跨域访问
public class TransportChildCvController {
    @Autowired
    private TransportChildCvService transportChildCvService;
    @Autowired
    private TransportChildCvService planCvService;
    @PostMapping("/pageList")
    public ResponseMsg<IPage<TransportChildCv>> pageList(@RequestBody TransportChildCv department) {

        return  transportChildCvService.pageList(department);
    }

    @PostMapping("/add")
    public ResponseMsg<Object> add(@RequestBody TransportChildCv department) {
        HttpServletRequest request = ServletUtils.getRequest();
        EmployeeVo empVo = request != null ? (EmployeeVo) request.getAttribute("empVo") : null;
        return  transportChildCvService.add(department,empVo);
    }

    @GetMapping("/detail/{id}")
    public ResponseMsg<Object> detail(@PathVariable String id) {
        HttpServletRequest request = ServletUtils.getRequest();
        EmployeeVo empVo = request != null ? (EmployeeVo) request.getAttribute("empVo") : null;
        return transportChildCvService.detail(id);
    }

    @RequestMapping(value = "importExcelxls")
    public ResponseMsg<Object> importExcelxls(@RequestParam("file") MultipartFile mFile) {
        ResponseMsg<Object> msg = new ResponseMsg<>();
        HttpServletRequest request = ServletUtils.getRequest();
        EmployeeVo empVo = request != null ? (EmployeeVo) request.getAttribute("empVo") : null;
        String fileName = mFile.getOriginalFilename();  //获取文件名称
        if (fileName.toLowerCase().endsWith(".xls") || fileName.toLowerCase().endsWith(".xlsx")) {
            Workbook wb = getWorkBook(mFile);
            return planCvService.importExcelxlAddOlan(wb, empVo);
        } else {
            msg.fail("请选择excel文件导入!");
            return msg;
        }
    }

    public Workbook getWorkBook(MultipartFile file) {
        //获得文件名
        String fileName = file.getOriginalFilename();
        //创建Workbook工作薄对象，表示整个excel
        Workbook workbook = null;
        try {
            //获取excel文件的io流
            InputStream is = file.getInputStream();
            //根据文件后缀名不同(xls和xlsx)获得不同的Workbook实现类对象
            if (fileName.endsWith("xls")) {
                //2003
                workbook = new HSSFWorkbook(is);
            } else if (fileName.endsWith("xlsx")) {
                //2007 及2007以上
                workbook = new XSSFWorkbook(is);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return workbook;
    }

    @PostMapping("/deployPageList")
    public ResponseMsg<IPage<TArea>> deployPageList(@RequestBody TransportChildCv transportChildCv)  {
        HttpServletRequest request = ServletUtils.getRequest();
        EmployeeVo empVo = request != null ? (EmployeeVo) request.getAttribute("empVo") : null;
        return transportChildCvService.wtcPageList(transportChildCv,empVo);
    }

    @PostMapping("/SupplierStatistics")
    public ResponseMsg<List<SupplierStatsDTO>> getSupplierStats(@RequestBody VehicleDeployCarCv cv) {
        return transportChildCvService.getSupplierStats(cv);
    }

    @PostMapping("/CustomerStatistics")
    public ResponseMsg<List<ConsignorStatsDTO>> getConsignorStats(@RequestBody TransportChildCv transportChildCv) {
        return transportChildCvService.getConsignorStats(transportChildCv);
    }

    @PostMapping("/VehicleStatistics")
    public ResponseMsg<List<VehicleStatistics>> getVehicleStats(@RequestBody TransportChildCv transportChildCv) {
//        List<TransportChildCv> stats = transportChildCvService.getConsignorStats();
//        return Result.success(stats);
        return transportChildCvService.getVehicleStats(transportChildCv);
    }
}
